<html>
<head><meta charset="utf-8"><title>WriteOnceCell · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/WriteOnceCell.html">WriteOnceCell</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="193963626"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/WriteOnceCell/near/193963626" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/WriteOnceCell.html#193963626">(Apr 14 2020 at 22:15)</a>:</h4>
<p>Hello! I'd like your opinion about the soundness / safety of this <code>WriteOnceCell</code>: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=6e220ff9a97663dc35f593d0cc8d4bd5" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=6e220ff9a97663dc35f593d0cc8d4bd5">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=6e220ff9a97663dc35f593d0cc8d4bd5</a></p>



<a name="193964461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/WriteOnceCell/near/193964461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/WriteOnceCell.html#193964461">(Apr 14 2020 at 22:24)</a>:</h4>
<p>Maybe using <code>MaybeUninit</code> is overkill? I just realized that <code>Option&lt;T&gt;</code> might be enough?</p>



<a name="193968851"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/WriteOnceCell/near/193968851" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DPC <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/WriteOnceCell.html#193968851">(Apr 14 2020 at 23:27)</a>:</h4>
<p><span class="user-mention" data-user-id="281572">@marmeladema</span> you can also ask <a class="stream" data-stream-id="146229" href="/#narrow/stream/146229-wg-secure-code">#wg-secure-code</a> <span aria-label="slight smile" class="emoji emoji-1f642" role="img" title="slight smile">:slight_smile:</span></p>



<a name="193971544"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/WriteOnceCell/near/193971544" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/WriteOnceCell.html#193971544">(Apr 15 2020 at 00:10)</a>:</h4>
<p><span class="user-group-mention" data-user-group-id="810">@WG-unsafe-code-guidelines</span>  is probably better for that</p>



<a name="193995891"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/WriteOnceCell/near/193995891" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/WriteOnceCell.html#193995891">(Apr 15 2020 at 08:00)</a>:</h4>
<p>Thank you! I will ask there then!</p>



<a name="194006811"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/WriteOnceCell/near/194006811" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/WriteOnceCell.html#194006811">(Apr 15 2020 at 09:50)</a>:</h4>
<p><span class="user-mention" data-user-id="281572">@marmeladema</span> yeah on first sight this seems good.<br>
To get some more assurance, you could write a few tests and run them in <a href="https://github.com/rust-lang/miri/" title="https://github.com/rust-lang/miri/">Miri</a> :D</p>



<a name="194007648"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/WriteOnceCell/near/194007648" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/WriteOnceCell.html#194007648">(Apr 15 2020 at 09:59)</a>:</h4>
<p>I definitely want to take a closer look at Miri, i've heard and read about it but never actually used it myself!</p>



<a name="194014017"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/WriteOnceCell/near/194014017" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/WriteOnceCell.html#194014017">(Apr 15 2020 at 11:09)</a>:</h4>
<p>That gave me an idea. Maybe we can somehow write a safe abstraction over Once to let you safely write into a mutable static because it knows it can only happen once</p>



<a name="194033803"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/WriteOnceCell/near/194033803" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/WriteOnceCell.html#194033803">(Apr 15 2020 at 13:55)</a>:</h4>
<p>What do people think about something along these lines? <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=9d4672b0cb186749c4a3ea977db0cce7" title="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=9d4672b0cb186749c4a3ea977db0cce7">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=9d4672b0cb186749c4a3ea977db0cce7</a><br>
I think it should be possible to decrease the size to 24 bytes and then it would truly be zero cost, but I'm not sure how to do it without heap allocation (being generic over <code>F: Fn() -&gt; T</code> is problematic because statics can't auto infer types)</p>



<a name="194039145"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/WriteOnceCell/near/194039145" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/WriteOnceCell.html#194039145">(Apr 15 2020 at 14:30)</a>:</h4>
<p>Is this the same functionality as <code>lazy_static</code>, modulo syntactic sugar?</p>



<a name="194039796"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/WriteOnceCell/near/194039796" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/WriteOnceCell.html#194039796">(Apr 15 2020 at 14:35)</a>:</h4>
<p>I'm unclear on the differences tween this and <a href="https://docs.rs/once_cell/1.3.1/once_cell/" title="https://docs.rs/once_cell/1.3.1/once_cell/">OnceCell</a>.</p>



<a name="194050170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/WriteOnceCell/near/194050170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/WriteOnceCell.html#194050170">(Apr 15 2020 at 15:41)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/WriteOnceCell/near/194039796" title="#narrow/stream/122651-general/topic/WriteOnceCell/near/194039796">said</a>:</p>
<blockquote>
<p>I'm unclear on the differences tween this and <a href="https://docs.rs/once_cell/1.3.1/once_cell/" title="https://docs.rs/once_cell/1.3.1/once_cell/">OnceCell</a>.</p>
</blockquote>
<p>Looks interesting, but I don't want to call <code>get_or_init</code> every time I want to access it.<br>
I find myself doing things like this quite a lot: <a href="https://play.rust-lang.org/?gist=51a178a221188189e6f711e062c83604" title="https://play.rust-lang.org/?gist=51a178a221188189e6f711e062c83604">https://play.rust-lang.org/?gist=51a178a221188189e6f711e062c83604</a></p>



<a name="194213722"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/WriteOnceCell/near/194213722" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/WriteOnceCell.html#194213722">(Apr 15 2020 at 19:11)</a>:</h4>
<p><span class="user-mention" data-user-id="249222">@Elichai Turkel</span>  I don't think I'm following. Isn't that literally</p>
<div class="codehilite"><pre><span></span><span class="k">use</span><span class="w"> </span><span class="n">once_cell</span>::<span class="n">sync</span>::<span class="n">OnceCell</span><span class="p">;</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">Context</span><span class="p">;</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Context</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">new</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="c1">// Do something computationally intensive.</span>
<span class="w">        </span><span class="n">Context</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">get_context</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nb">&#39;static</span><span class="w"> </span><span class="n">Context</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">static</span><span class="w"> </span><span class="n">INSTANCE</span>: <span class="nc">OnceCell</span><span class="o">&lt;</span><span class="n">Context</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">OnceCell</span>::<span class="n">new</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="n">INSTANCE</span><span class="p">.</span><span class="n">get_or_init</span><span class="p">(</span><span class="n">Context</span>::<span class="n">new</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="194213887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/WriteOnceCell/near/194213887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/WriteOnceCell.html#194213887">(Apr 15 2020 at 19:13)</a>:</h4>
<p>Or</p>
<div class="codehilite"><pre><span></span><span class="k">use</span><span class="w"> </span><span class="n">once_cell</span>::<span class="n">sync</span>::<span class="n">Lazy</span><span class="p">;</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">Context</span><span class="p">;</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Context</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">new</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="c1">// Do something computationally intensive.</span>
<span class="w">        </span><span class="n">Context</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">static</span><span class="w"> </span><span class="n">CONTEXT</span>: <span class="nc">Lazy</span><span class="o">&lt;</span><span class="n">Context</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Lazy</span>::<span class="n">new</span><span class="p">(</span><span class="n">Context</span>::<span class="n">new</span><span class="p">);</span><span class="w"></span>
</pre></div>



<a name="194215477"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/WriteOnceCell/near/194215477" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/WriteOnceCell.html#194215477">(Apr 15 2020 at 19:27)</a>:</h4>
<p>hehe, you're right. if you plug it into my design then it does look good and free of unsafe :)<br>
Oh and Lazy does exactly that ha. <br>
How does it compile without explictly specifying the exact type? that did not work for me</p>



<a name="194217115"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/WriteOnceCell/near/194217115" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/WriteOnceCell.html#194217115">(Apr 15 2020 at 19:41)</a>:</h4>
<p>oh because of the equal sign in <code>F = fn() -&gt; T</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>